import { Component, OnInit } from '@angular/core';
import { HttpClient,HttpHeaders } from '@angular/common/http';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';

@Component({
  selector: 'app-table',
  templateUrl: './table.component.html',
  styleUrls: ['./table.component.scss']
})
export class TableComponent implements OnInit {

 
  operationData: any = [];
  pageSize = 10 //页面大小
  pageTotal : any; //页面总数据
  pageIndex = 1 //当前页码
  //查询条件
  check: any = [{
    personnelName:'',
    personnelCondition:'',
    personnelDepartment:''
  }];
  //通过ID查询到的数据
  byIdData :any;
  //新增or修改的临时数据
  temporaryData:any = {
    personnelId: 0,
    personnelName : '',
    personnelSex: 0,
    personnelBirthday: '',
    personnelCondition: '',
    personnelAcademicDegree: '',
    personnelDepartment: '',
    personnelPost: '',
    personnelSalary: ''
    
  };

  isVisible = false; //对话框开关
  isConfirmLoading = false;

  visible = false; //抽屉开关
  ruleTitle = ''; //抽屉标题

  id:any;

  constructor(
    public httpclicent: HttpClient,
    
  ) { }

  ngOnInit(): void {
    this.httpclicent.get('http://localhost:8082/personnel/query/'+this.pageIndex+'/'+this.pageSize).subscribe((response:any)=>{
      
      if(response.code == '001'){
        this.operationData = response.data.list;
        this.pageSize = response.data.pageSize;
        this.pageIndex = response.data.pageNum;
        this.pageTotal = response.data.pages * response.data.pageSize;
      }
      
    })
    
  }

  //页码改变的回调
  nzPageIndexChange(){
    if (this.check.personnelNamea == undefined) {
      this.check.personnelName = '';
    }
    if (this.check.personnelCondition == undefined) {
      this.check.personnelCondition = '';
    }
    if (this.check.personnelDepartment == undefined) {
      this.check.personnelDepartment = '';
    }
    this.httpclicent.get('http://localhost:8082/personnel/check?pageIndx='+this.pageIndex+'&pageSize='+this.pageSize+'&personnelName='+this.check.personnelName+'&personnelCondition='+this.check.personnelCondition+'&personnelDepartment='+this.check.personnelDepartment)
    .subscribe((response:any)=>{
      
      if(response.code == '001'){
        this.operationData = response.data.list;
        this.pageSize = response.data.pageSize;
        this.pageIndex = response.data.pageNum;
        this.pageTotal = response.data.pages * response.data.pageSize;
      }
    })
  }

  //每页条数改变的回调
  nzPageSizeChange(){
    if (this.check.personnelNamea == undefined) {
      this.check.personnelName = '';
    }
    if (this.check.personnelCondition == undefined) {
      this.check.personnelCondition = '';
    }
    if (this.check.personnelDepartment == undefined) {
      this.check.personnelDepartment = '';
    }
    this.httpclicent.get('http://localhost:8082/personnel/check?pageIndx='+this.pageIndex+'&pageSize='+this.pageSize+'&personnelName='+this.check.personnelName+'&personnelCondition='+this.check.personnelCondition+'&personnelDepartment='+this.check.personnelDepartment)
    .subscribe((response:any)=>{
      
      if(response.code == '001'){
        this.operationData = response.data.list;
        this.pageSize = response.data.pageSize;
        this.pageIndex = response.data.pageNum;
        this.pageTotal = response.data.pages * response.data.pageSize;
      }
    })
  }
  //搜索功能
  sumbit(){
    if (this.check.personnelName == undefined) {
      this.check.personnelName = '';
    }
    
    if (this.check.personnelCondition == undefined) {
      this.check.personnelCondition = '';
    }
    if (this.check.personnelDepartment == undefined) {
      this.check.personnelDepartment = '';
    }
    //console.log(this.check);
    
    this.httpclicent.get('http://localhost:8082/personnel/check?pageIndx='+this.pageIndex+'&pageSize='+this.pageSize+'&personnelName='+this.check.personnelName+'&personnelCondition='+this.check.personnelCondition+'&personnelDepartment='+this.check.personnelDepartment)
    .subscribe((response:any)=>{
      
      if(response.code == '001'){
        this.operationData = response.data.list;
        this.pageSize = response.data.pageSize;
        this.pageIndex = response.data.pageNum;
        this.pageTotal = response.data.pages * response.data.pageSize;
      }
    })
    
    
  }
  //重置按钮
  reset(){
    this.check= [{
      personnelName:'',
      personnelCondition:'',
      personnelDepartment:''
    }];
  }

  update(id:any){
    this.visible = true;
    this.id = id;
    this.ruleTitle = '编辑人员';
    this.httpclicent.get('http://localhost:8082/personnel/'+this.id).subscribe((res)=>{
      this.byIdData = res;
      this.temporaryData = this.byIdData;
      
   
      
    
    });
    
    
  }

  //关闭抽屉
  close() {
    this.visible = false;
  }

  add(){
    this.id = '';
    this.visible = true;
    this.ruleTitle = '新增人员';
    
    this.temporaryData.personnelName = '';
    this.temporaryData.personnelSex = 1;
    this.temporaryData.personnelBirthday = '';
    this.temporaryData.personnelAcademicDegree = '';
    this.temporaryData.personnelDepartment = '';
    this.temporaryData.personnelPost = '';
    this.temporaryData.personnelSalary = '';
    this.temporaryData.personnelCondition = '';
  }

  //提交新增人员
  addData(){
    if(this.ruleTitle == '新增人员'){
      const httpOptions = {
        headers: new HttpHeaders({ 'Content-Type': 'application/json' })
      };
      console.log(this.temporaryData);
      
      this.httpclicent.post('http://localhost:8082/personnel',this.temporaryData,httpOptions).subscribe((res:any)=>{
      });
      this.visible = false;

      alert("新增成功")
      //刷新
      this.refresh();
    }else{
      const httpOptions = {
        headers: new HttpHeaders({ 'Content-Type': 'application/json' })
      };
      console.log(this.temporaryData);
      this.httpclicent.put('http://localhost:8082/personnel',this.temporaryData,httpOptions).subscribe((res:any)=>{
      });
      alert("修改成功");
      this.visible = false;
      this.refresh();

    }
    
  }

  //删除对话框  取消
  handleCancel(): void {
    this.isVisible = false;
  }
  //点击列表中的删除
  showModal(id:any): void {
    this.id = id;

    this.isVisible = true;
  }
  //删除对话框  确定
  handleOk(): void {
    this.httpclicent.delete('http://localhost:8082/personnel/'+this.id).subscribe((res:any)=>{})
    this.isVisible = false;
    this.refresh();
    
  }

  //刷新功能
  refresh(){
    this.httpclicent.get('http://localhost:8082/personnel/query/'+this.pageIndex+'/'+this.pageSize).subscribe((response:any)=>{
        if(response.code == '001'){
          this.operationData = response.data.list;
          this.pageSize = response.data.pageSize;
          this.pageIndex = response.data.pageNum;
          this.pageTotal = response.data.pages * response.data.pageSize;
        }
      });
  }
}


